package com.burry.action;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import applelew.util.Debug;

import com.burry.bo.JSONResult;
import com.burry.services.ContentService;

public class ContentAction extends BaseAction {
	
	private String id;
	
	private String type;
	private String title;
	private String content;
	
	private InputStream inputStream;
	
	public InputStream getInputStream() {
		return inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String listContent(){
		ContentService cs = new ContentService();
		List goodList = cs.getAllContent();
		
		JSONResult json = new JSONResult(goodList);
		JSONObject jsonRet = JSONObject.fromObject(json);  
		String allSize = jsonRet.toString();

		try {
			setInputStream(new ByteArrayInputStream(allSize.getBytes("utf-8")));
		} catch (UnsupportedEncodingException e) {
			Debug.logError(e.getMessage());
		}
		
		return SUCCESS;
	}
	
	public String saveContent(){
		ContentService cs = new ContentService();
		cs.addAContent(type, title, content);
		return SUCCESS;
	}
	
	public String delContent(){
		ContentService cs = new ContentService();
		cs.deleteAContent(id);
		return SUCCESS;
	}
	
	public String updateContent(){
		ContentService cs = new ContentService();
		Map result = cs.updateContent(id, type, title, content);
		Debug.logInfo("update content result ==> " + result);
		return SUCCESS;
	}
	
	public String showContent(){
		ContentService cs = new ContentService();
		Map result = cs.getAContent(id);
		this.title = (String)result.get("title");
		this.type = (String)result.get("type");
		this.content = (String)result.get("content");
		return SUCCESS;
	}
}
